package com.zhai.domain.activity.service.quota.rule.impl;

import com.zhai.domain.activity.model.entity.ActivityCountEntity;
import com.zhai.domain.activity.model.entity.ActivityEntity;
import com.zhai.domain.activity.model.entity.ActivitySkuEntity;
import com.zhai.domain.activity.model.valobj.ActivityStateVO;
import com.zhai.domain.activity.service.quota.rule.AbstractActionChain;
import com.zhai.types.enums.ResponseCode;
import com.zhai.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description: 活动规则过滤
 * @ClassName: ActivityBaseActionChain
 * @Author: zhaiyongxin
 * @Date: 2025/2/20 11:26
 * @Version: 1.0
 */
@Component("activity_base_action")
@Slf4j
public class ActivityBaseActionChain extends AbstractActionChain {

    /**
     * 执行责任链
     *
     * @param activitySkuEntity
     * @param activityEntity
     * @param activityCountEntity
     * @return
     */
    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        log.info("活动责任链-基础信息【有效期、状态、库存(sku】校验开始。sku:{} activityId:{}", activitySkuEntity.getSku(), activitySkuEntity.getActivityId());

        // 校验活动状态
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        // 校验活动时间 (开始时间 <= 当前时间 <= 结束时间)
        Date currentDate = new Date();
        if (currentDate.before(activityEntity.getBeginDateTime()) || currentDate.after(activityEntity.getEndDateTime())) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        // 校验；活动sku库存 「剩余库存从缓存获取的」
        if (activitySkuEntity.getStockCountSurplus() <= 0){
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(), ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }
        return next().action(activitySkuEntity, activityEntity, activityCountEntity);
    }
}
